package vn.wallet.app.dao.impl;

// Generated Oct 23, 2012 10:05:44 PM by Hibernate Tools 3.4.0.CR1

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import vn.wallet.app.dao.IUserDAO;
import vn.wallet.app.domain.User;

@Repository
public class UserDAO extends GenericDAO<User> implements IUserDAO {

	public UserDAO() {
		super(User.class);
	}

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public User validateUserLogin(String username, String password, String ip) {
		String hql = "from User where username = :user and password = :pass";
		try {
			Object obj = getSessionFactory().getCurrentSession()
					.createQuery(hql)
					.setString("user", username)
					.setString("pass", password)
					.uniqueResult();
			if (obj != null && obj instanceof User) {
				User user = (User) obj;

				user.setLastvisitDate(new Date());
				user.setIpLogined(ip);
				update(user);

				return user;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	@Override
	public User getById(int id) throws Exception {
		return (User) getSessionFactory().getCurrentSession().get(User.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getByName(String name) throws Exception {
		Criteria crit = getSessionFactory().getCurrentSession().createCriteria(User.class);
		crit.add(Restrictions.like("username", "%" + name + "%"));

		return crit.list();
	}

	@Override
	public void save(User obj) throws Exception {
		getSessionFactory().getCurrentSession().persist(obj);
	}

	@Override
	public void update(User obj) throws Exception {
		getSessionFactory().getCurrentSession().merge(obj);
	}

	@Override
	public void delete(User obj) throws Exception {
		getSessionFactory().getCurrentSession().delete(obj);
	}

	@Override
	public void delete(int id) throws Exception {
		getSessionFactory().getCurrentSession().delete(getById(id));
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getAll() throws Exception {
		return getSessionFactory().getCurrentSession().createQuery("from User").list();
	}

	@Override
	public int getCount() throws Exception {
		return (Integer) getSessionFactory().getCurrentSession().createQuery("select count(*) from User").uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findByExample(User obj) throws Exception {
		List<User> lst = null;
		Example ex = Example.create(obj);
		ex.enableLike(MatchMode.ANYWHERE);
		ex.ignoreCase();

		lst = getSessionFactory().getCurrentSession().createCriteria(User.class)
				.add(ex)
				.list();

		return lst;
	}

	@Override
	public List<User> search() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
